# coding=utf-8
"""
作者：董新强 
创建时间：2020-8-11
描述：参数评估
"""
from sqlalchemy import desc

from db_models.cat_abilities import CatAbilities
from db_models.cat_questions import CatQuestions
from db_models.orm_base import OrmBase
from db_models.param_estimate import ParamEstimate
from db_models.param_estimate_details import ParamEstimateDetails
from db_models.updated_questions import UpdatedQuestions
from framework.db_session import DbSession
from framework.msg import failure, ok
from framework.utilities import timestamp, time_str
from services.admin.admin_base import AdminBase


class ParamsManage(AdminBase):
    def __init__(self, usr):
        super().__init__(usr)

    @AdminBase.admin(failure(msg='获取评估失败'))
    def get_list(self, aid):
        ls = []
        with DbSession.create() as db:
            query = db.query(ParamEstimate.id, ParamEstimate.create_time, ParamEstimate.status, CatAbilities.name) \
                .filter(ParamEstimate.aid == aid) \
                .filter(CatAbilities.id == ParamEstimate.aid) \
                .order_by(desc(ParamEstimate.id))
            for id, tm, status, name in query:
                ls.append({
                    'id': id,
                    'time': time_str(tm),
                    'status': status,
                    'name': name
                })
        return ok(data=ls)

    @AdminBase.admin(failure(msg='获取评估失败'))
    def get_detail_list(self, est_id):
        ls = []
        with DbSession.create() as db:
            query = db.query(ParamEstimateDetails).filter(ParamEstimateDetails.pid == est_id).order_by(desc(ParamEstimateDetails.id))
            for v in query:
                ls.append(OrmBase.get_dict(v))
        return ok(data=ls)

    @AdminBase.admin(failure(msg='更新参数失败'))
    def update(self, est_id):
        '''
        更新评估的参数到试题.
        :param est_id: ParamEstimate.id
        :return:
        '''
        now = timestamp()
        with DbSession.create() as db:
            est = db.query(ParamEstimate).filter(ParamEstimate.id == est_id).first()
            if not est:
                return failure(msg='评估不存在')

            if est.status == 1:
                return failure(msg='已导入该评估')

            details = db.query(ParamEstimateDetails.qid, ParamEstimateDetails.a_new, ParamEstimateDetails.b_new) \
                .filter(ParamEstimateDetails.pid == est_id).all()
            for qid, a, b in details:
                db.query(CatQuestions).filter(CatQuestions.id == qid).update({
                    CatQuestions.a: a,
                    CatQuestions.b: b
                })

                UpdatedQuestions.add(qid, now)
            est.status = 1
        return ok(msg='导入成功')

    @AdminBase.admin(failure(msg='删除评估失败'))
    def delete(self, est_id):
        '''
        更新评估的参数到试题.
        :param est_id: ParamEstimate.id
        :return:
        '''
        with DbSession.create() as db:
            est = db.query(ParamEstimate).filter(ParamEstimate.id == est_id).first()
            if not est:
                return ok(msg='评估不存在')

            if est.status == 1:
                return failure(msg='已导入该评估')

            db.delete(est)
        return ok(msg='导入成功')
